package com.yangguang.huahui.ui.activitys;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseQuickAdapter;
import com.yangguang.huahui.baseadapter.BaseViewHolder;
import com.yangguang.huahui.entity.Quotes;
import com.yangguang.huahui.mvp.presenter.QuotesListPresenter;
import com.yangguang.huahui.mvp.view.QuotesListView;
import com.yangguang.huahui.ui.adapters.BGABannerAdapter;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;

public class QuotesActivity extends BaseMvpActivity<QuotesListView, QuotesListPresenter> implements QuotesListView, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.tv_title)
    TextView tv_title;
    private int page;
    @BindView(R.id.mSwip)
    SwipeRefreshLayout mSwip;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private LinearLayoutManager layoutManager;
    private QuotesAdapter mAdapter;
    private View headView;

    private BGABanner mBgaBanner;

    @OnClick({R.id.iv_back})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_quotes;
    }

    @Override
    protected void initView() {
        tv_title.setText(getString(R.string.market_quote));
        mSwip.setColorSchemeResources(R.color.main_color, R.color.main_color, R.color.main_color);
        mSwip.setOnRefreshListener(this);
        layoutManager = new LinearLayoutManager(mContext);
        mRecyclerView.setLayoutManager(layoutManager);
        //实现首次自动显示加载提示
        mSwip.post(new Runnable() {
            @Override
            public void run() {
                mSwip.setRefreshing(true);
            }
        });
        headView = getLayoutInflater().inflate(R.layout.bgabanner_head_layout, null);
        mBgaBanner = (BGABanner) headView.findViewById(R.id.mBgaBanner);
        mAdapter = new QuotesAdapter(R.layout.quotes_list_item, null);
        mAdapter.addHeaderView(headView);
        //设置加载动画
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        //将适配器添加到RecyclerView
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(Quotes result) {
        if (result.getList() != null) {
            List<String> imgs = new ArrayList<>();
            List<String> titles = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                imgs.add(result.getList().get(i).getIcon());
                titles.add(result.getList().get(i).getTitle());
            }
            mBgaBanner.setAdapter(new BGABannerAdapter(mContext));
            mBgaBanner.setData(imgs, titles);
            List<Quotes.ListBean> datas = new ArrayList<>();
            if (result.getList().size() > 3) {
                for (int i = 3; i < result.getList().size() - 3; i++) {
                    datas.add(result.getList().get(i));
                }
                if (page > 0) {
                    mAdapter.addData(datas);//新增数据
                    mAdapter.notifyDataChangedAfterLoadMore(true);
                } else {
                    mAdapter.setNewData(result.getList());
                    mAdapter.openLoadMore(result.getList().size(), true);//设置是否可以下拉加载  以及加载条数
                }
            }
        }
//        else {
//            if (page > 0) {
//                //所有数据加载完成后显示
//                try {
//                    mAdapter.notifyDataChangedAfterLoadMore(false);
//                    View view = getLayoutInflater().inflate(R.layout.layout_end_view, (ViewGroup) mRecyclerView.getParent(), false);
//                    mAdapter.addFooterView(view);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
        mSwip.setRefreshing(false);
    }

    @Override
    public void onError() {

    }

    @Override
    protected QuotesListPresenter initPresenter() {
        return new QuotesListPresenter();
    }

    @Override
    protected void fetchData() {
        mPresenter.getList(page);
    }

    @Override
    public void onRefresh() {
        page = 0;
        fetchData();
    }

    private class QuotesAdapter extends BaseQuickAdapter<Quotes.ListBean> {

        public QuotesAdapter(int layoutResId, List<Quotes.ListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Quotes.ListBean item) {
            helper.setText(R.id.tv_title, item.getTitle());
            helper.setText(R.id.tv_date, CommonUtil.longToTime(Long.parseLong(item.getCreatetime() + "000"), 1006));
            helper.setText(R.id.tv_kind, item.getType());
            helper.setText(R.id.tv_like, item.getLike());
            ImageLoader.load(mContext, item.getIcon(), (ImageView) helper.getView(R.id.iv_right));
        }
    }

    private class BGABannerAdapter implements BGABanner.Adapter {
        private Context context;

        public BGABannerAdapter(Context context) {
            this.context = context;
        }

        @Override
        public void fillBannerItem(BGABanner banner, View view, Object model, int position) {
            Glide.with(context)
                    .load(model)
                    .crossFade()
                    .placeholder(R.mipmap.home_load_error)
                    .centerCrop()
                    .into((ImageView) view);

        }
    }
}
